package com.yuedao.sender.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.ydx.protobuf.OTIpcDef;

import com.yuedao.sender.model.OperateArrive;
import com.yuedao.sender.model.OperateDepart;
import com.yuedao.sender.model.OperateLogin;
import com.yuedao.sender.model.OperateLogout;
import com.yuedao.sender.model.OperatePay;
import com.yuedao.sender.service.OperateArriveRepository;
import com.yuedao.sender.service.OperateDepartRepository;
import com.yuedao.sender.service.OperateLoginRepository;
import com.yuedao.sender.service.OperateLogoutRepository;
import com.yuedao.sender.service.OperatePayRepository;
import com.yuedao.sender.service1.OperateService;
import com.yuedao.sender.util.AppLogger;
import com.yuedao.sender.util.ByteUtil;
import com.yuedao.sender.util.HttpSender;

@RestController
@RequestMapping("/sendoperinfo")
public class OperateController {

	@Autowired
	OperateArriveRepository operateArriveRepository;

	@Autowired
	OperateDepartRepository operateDepartRepository;

	@Autowired
	OperateLoginRepository operateLoginRepository;

	@Autowired
	OperateLogoutRepository operateLogoutRepository;

	@Autowired
	OperatePayRepository operatePayRepository;
	
	@Autowired
	OperateService  opers;
	
	protected static AppLogger logger = AppLogger.getInstance();
	
	@RequestMapping("/operateArrive")
	public String sendOperateArrive(){
		List<OperateArrive> list = new ArrayList<OperateArrive>();
		String message = null;
		try {
			list = operateArriveRepository.findBySendFlag(0);
			
			message=opers.sendOperateArrive(list);
			//将发送过的数据标志字段(send_flag)更新为1
			for(int i=0;i < list.size();i++){
				list.get(i).setSendFlag(1);
			}
			operateArriveRepository.save(list);
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return message;
	}
	
	
	
	@RequestMapping("/operateDepart")
	public String sendOperateDepart(){
		List<OperateDepart> list = new ArrayList<OperateDepart>();
		String message = null;
		try {
			list = operateDepartRepository.findBySendFlag(0);
			
			message=opers.sendOperateDepart(list);
			
			//将发送过的数据标志字段(send_flag)更新为1
			for(int i=0;i < list.size();i++){
				list.get(i).setSendFlag(1);
			}
			operateDepartRepository.save(list);
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return message;
	}
	
	
	
	
	@RequestMapping("/operateLogin")
	public String sendOperateLogin(){
		List<OperateLogin> list = new ArrayList<OperateLogin>();
		String message = null;
		try {
			list = operateLoginRepository.findBySendFlag(0);

			message=opers.sendOperateLogin(list);
			
			//将发送过的数据标志字段(send_flag)更新为1
			for(int i=0;i < list.size();i++){
				list.get(i).setSendFlag(1);
			}
			operateLoginRepository.save(list);
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return message;
	}

	
	@RequestMapping("/operateLogout")
	public String sendOperateLogout(){
		List<OperateLogout> list = new ArrayList<OperateLogout>();
		String message = null;
		try {
			list = operateLogoutRepository.findBySendFlag(0);

			message=opers.sendOperateLogout(list);
			//将发送过的数据标志字段(send_flag)更新为1
			for(int i=0;i < list.size();i++){
				list.get(i).setSendFlag(1);
			}
			operateLogoutRepository.save(list);
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return message;
	}
	
	
	@RequestMapping("/operatePay")
	public String sendOperatePay(){
		List<OperatePay> list = new ArrayList<OperatePay>();
		String message = null;
		try {
			list = operatePayRepository.findBySendFlag(0);

			message=opers.sendOperatePay(list);
			//将发送过的数据标志字段(send_flag)更新为1
			for(int i=0;i < list.size();i++){
				list.get(i).setSendFlag(1);
			}
			operatePayRepository.save(list);
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return message;
	}
	
}
